<template>
  <a-modal v-model:visible="visible" title="补偿时长" @before-ok="onOk">
    <a-form ref="formRef" size="medium" auto-label-width>
      <a-form-item label="延长天数" field="days">
        <a-input-group>
          <a-select
            :style="{ width: 'fit-content' }"
            :trigger-props="{ autoFitPopupMinWidth: true }"
            v-model="daysType"
            @change="getTopData"
          >
            <a-option :value="1" label="按日期" />
            <a-option :value="2" label="按天数" />
          </a-select>
          <template v-if="daysType == 1">
            <a-date-picker
              v-model="date"
              show-time
              :time-picker-props="{ defaultValue: '23:59:59' }"
              :disabledDate="(current) => dayjs(current).isBefore(dayjs().startOf('date'))"
            >
              <a-input :model-value="date || '请选择日期'"></a-input>
            </a-date-picker>
          </template>
          <template v-else>
            <a-input-number v-model="days" :min="1"></a-input-number>
          </template>
        </a-input-group>
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script setup>
import { accountRenewUserWechat } from '@/apis'
import { Message } from '@arco-design/web-vue'
import dayjs from 'dayjs'

const emit = defineEmits(['getTableData', 'restore'])

const visible = ref(false)
const userArr = ref([])
const days = ref()
const date = ref()
const daysType = ref(1)

// 补偿时长
const compensate = (keys) => {
  userArr.value = keys
  visible.value = true
}

// 确认补偿时长
const onOk = async () => {
  if (daysType.value == 1 && !date.value) {
    Message.error('请选择日期')
    return false
  }
  if (daysType.value == 2 && !days.value) {
    Message.error('请输入天数')
    return false
  }
  const apiArr = []
  userArr.value.forEach((userArrItem) => {
    const userArrItemF = userArrItem.split('☆')
    let diffDay = days.value
    if (daysType.value == 1) {
      diffDay = dayjs(date.value).diff(dayjs(userArrItemF[1]), 'day') + 1
      if (diffDay < 0) diffDay = 0
    }
    apiArr.push(accountRenewUserWechat({ days: diffDay, userWechatId: userArrItemF[0] }))
  })
  await Promise.all(apiArr)
  Message.success('补偿成功')
  userArr.value = []
  emit('getTableData')
  emit('restore')
  return true
}

defineExpose({ compensate })
</script>
<style scoped lang="scss">
.textd43030 {
  color: #d43030;
}
</style>
